/**
  * @file prot2ppm.cpp
  */
  
  /* Copyright 2010 Antonio Ruano Cuesta
 *
 * This program is free software: you can redistribute it and/or modify it under 
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later 
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with 
 * this program. If not, see <http://www.gnu.org/licenses/>
*/

#include <new>
#include <fstream>
#include <iostream>
#include "imagenppm.h"
#include "cto_prototipo.h"
#include "func_aux.h"

using namespace std;

/**
  * @brief Funcion principal que gestiona la transformacion de un cojunto de prototipos  
  * a una imagen PPM.
  */
int main (int argc, char *argv[])
{
	if (argc != 6){
		cerr<<"Numero de argumentos incorrecto."<<endl;
		cerr<<"Formato de la llamada: prot2ppm fich_prot fich_map filas cols fich_ppm"
			<<endl;
		exit(1);
	}

	int filas, cols;
	char * fich_prot, *fich_map, *fich_ppm;

	fich_prot = argv[1];
	fich_map = argv[2];
	filas = atol(argv[3]);
	cols = atol(argv[4]);
	fich_ppm = argv[5];

	if (filas <= 0 || cols <= 0){
		cerr<<"El numero de filas o columnas ha de ser positivo"<<endl;
		exit (1);
	}

	cto_prot prototipos;

	prototipos.InsertarDesdeFichero (fich_prot);

	if (filas*cols != (int)prototipos.get_numprot()){
		cerr<<"El numero de prototipos del fichero debe tener el mismo valor que filas*cols."
			<<endl;
		exit (1);
	}

	if (prototipos.get_numclas() == 0){
		cerr<<fich_prot<<" no esta clasificado. Debe clasificarse primero."
			<<endl;
		exit (1);
	}

	int **mapa = LeerMapaColor (fich_map, prototipos.get_numclas());
	bool exito;

	ImagenPPM imag = prototipos.ConversionAPPM(mapa,filas,cols);

	exito = imag.EscribeImagenPPM (fich_ppm);

	if (exito)
		cout<<"Imagen PPM generada correctamente."<<endl;
	else
		cerr<<"Error al crear la imagenPPM."<<endl;

	for (unsigned int i = 0 ; i < prototipos.get_numclas() ; i++)
		delete [] mapa[i];

	delete mapa;
	
		
}

